package ${packageName}.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import ${packageName}.mapper.${mapperName};
import ${packageName}.domain.${entityName};
import ${packageName}.service.${serviceName};
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.water.conservancy.common.exception.ServiceException;
import ${packageName}.dto.req.add.${addReqDto};
import ${packageName}.dto.req.update.${updateReqDto};
import ${packageName}.dto.req.query.${queryPageReqDto};
import ${packageName}.dto.res.${queryResDto};
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Transactional(rollbackFor = Exception.class, noRollbackFor = ServiceException.class)
public class ${serviceImplName} extends ServiceImpl<${mapperName},${entityName}> implements ${serviceName} {

    /**
     * 分页查询
     */
    @Override
    public IPage<${queryResDto}> query${entityName}Page(${queryPageReqDto} reqDto) {

        LambdaQueryWrapper<${entityName}> queryWrapper = Wrappers.lambdaQuery(${entityName}.class)
            .orderByDesc(${entityName}::getCreateTime);

        Page<${entityName}> page = baseMapper.selectPage(reqDto.iPageInfo(), queryWrapper);
        if (BeanUtil.isEmpty(page) || CollUtil.isEmpty(page.getRecords())) {
            return new Page<>();
        }

        // TODO 是否需要做字段填充
        return page.convert(${entityNameLower} -> {
            ${queryResDto} queryResDto = new ${queryResDto}();
            BeanUtils.copyProperties(${entityNameLower}, queryResDto);
            return queryResDto;
        });
    }

    /**
     * 添加
     */
    @Override
    public ${entityName} add${entityName}(${addReqDto} reqDto) {
        // TODO 是否需要做校验

        ${entityName} ${entityNameLower} = new ${entityName}();
        BeanUtils.copyProperties(reqDto, ${entityNameLower});

        this.save(${entityNameLower});
        return ${entityNameLower};
    }

    /**
     * 编辑
     */
    @Override
    public Boolean update${entityName}(${updateReqDto} reqDto) {
        // TODO 是否需要做校验

        ${entityName} ${entityNameLower} = this.getById(reqDto.getId());
        if (BeanUtil.isEmpty(${entityNameLower})) {
            throw new ServiceException("编辑信息不存在");
        }

        ${entityName} update${entityName} = new ${entityName}();
        BeanUtils.copyProperties(reqDto, update${entityName});
        update${entityName}.setId(${entityNameLower}.getId());
        return this.updateById(update${entityName});
    }

    /**
     * 批量删除
     */
    @Override
    public Boolean deleteBatch(List<Long> ids){
        if(CollUtil.isEmpty(ids)){
            throw new ServiceException("请选择要删除的数据");
        }

        // TODO 是否需要做校验

        return this.removeByIds(ids);
    }
}